//
//  MYGKeyanzhichiViewController.swift
//  MYDoctor
//
//  Created by 王振明 on 2024/4/18.
//

import UIKit

class MYGKeyanzhichiViewController: MYGBaseController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "科研支持"
        navigationBarShow()
        initUI()
    }
    @objc func viewAction(tap:UITapGestureRecognizer){
        switch tap.view?.tag {
        case 10086:
            checkDoctorInfoIsComplete()
            break
        case 10087:
            let vc = H5ViewController()
            vc.title = "临床试验"
            let token = MYGUserManager.getUser()?.verification_token ?? ""
            vc.url = "http://h5-dev.medataplus.com/#/pages/clinicalDetail/clinicalDetail?token=\(token)"
            self.navigationController?.pushViewController(vc)
        case 10088:
            let vc = H5ViewController()
            vc.title = "文献查询"
            vc.url = "https://www.peipusci.com/sci?type=27"
            self.navigationController?.pushViewController(vc)
        default:
            break
        }
    }
    
    func checkDoctorInfoIsComplete(){
        
        if ((MYGUserManager.getUser()?.doctor.name.isEmpty) == true) || MYGUserManager.getUser()?.doctor.gender == 0 || ((MYGUserManager.getUser()?.doctor.identity.isEmpty) == true) {
            
            let vc = MYGBaseInfoController()
//            vc.model = model
            self.navigationController?.pushViewController(vc)
            
            return
        }
        
        let phone = MYGUserManager.getUser()?.doctor.phone ?? 0
        MMYGNetwork<MYGMicrosrationApi,MYGUserDoctor>.request(target: .get_doctor_phone_info(phone: "\(phone)")) { model  in
            MYGLoading.hide()
            guard let item = model else {return}
            
            let user = MYGUserManager.getUser()
            user?.doctor = item
            MYGUserManager.saveUser(user!)
//            
//            let vc = MYGUpdateInfoViewController()
//            vc.model = user
//            self.navigationController?.pushViewController(vc)
            
            
            if MYGUserManager.isDoctorInfComplete{
                if MYGUserManager.getUser()?.doctor.status == 20 {
                    self.navigationController?.pushViewController(MYGPatientListViewController())
                }else {
                    MMYGHUD.showToast(text: "正在审核中，暂时无法使用该功能")
                }
            }else {
                let vc = MYGUpdateInfoViewController()
                vc.model = MYGUserManager.getUser()!
                vc.isEdit = true
                self.navigationController?.pushViewController(vc)
            }
            
        } failed: { _, error in
            MYGLoading.hide()
            MMYGHUD.showToast(text: error)
        }
        
        
        
    }
    
    private func initUI(){
        view.addSubview(keyanzhichi)
        keyanzhichi.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalToSuperview().offset(SCALEWIDTH(20))
            make.right.equalToSuperview().offset(-SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(72))
        }
        view.addSubview(clinTryView)
        clinTryView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalTo(keyanzhichi.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(72))
        }
        view.addSubview(docufindView)
        docufindView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(20))
            make.top.equalTo(clinTryView.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-SCALEWIDTH(20))
            make.height.equalTo(SCALEWIDTH(72))
        }
    }
    private lazy var keyanzhichi:UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.layer.shadowColor = RGBA(r: 200, g: 200, b: 200, a: 0.5).cgColor
        vi.layer.shadowOffset = CGSize(width: 2, height: 2)
        vi.layer.shadowRadius = 4.5
        vi.layer.shadowOpacity = 1
        vi.layer.cornerRadius = 7
        vi.tag = 10086
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewAction(tap:)))
        vi.addGestureRecognizer(tap)
        let imageV = UIImageView()
        imageV.image = UIImage(named: "science_support")
        vi.addSubview(imageV)
        imageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(18.5))
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: SCALEWIDTH(50), height: SCALEWIDTH(50)))
        }
        let lab = UILabel()
        lab.font = .medium(SCALEWIDTH(15))
        lab.textColor = .rgba(34, 34, 34,1)
        lab.text = "患者管理"
        vi.addSubview(lab)
        lab.snp.makeConstraints { make in
            make.left.equalTo(imageV.snp.right).offset(SCALEWIDTH(20))
            make.centerY.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(21))
        }
        return vi
    }()
    
    private lazy var clinTryView:UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.layer.shadowColor = RGBA(r: 200, g: 200, b: 200, a: 0.5).cgColor
        vi.layer.shadowOffset = CGSize(width: 2, height: 2)
        vi.layer.shadowRadius = 4.5
        vi.layer.shadowOpacity = 1
        vi.layer.cornerRadius = 7
        vi.tag = 10087
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewAction(tap:)))
        vi.addGestureRecognizer(tap)
        let imageV = UIImageView()
        imageV.image = UIImage(named: "MYG_home_clinTry")
        vi.addSubview(imageV)
        imageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(18.5))
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: SCALEWIDTH(50), height: SCALEWIDTH(50)))
        }
        let lab = UILabel()
        lab.font = .medium(SCALEWIDTH(15))
        lab.textColor = .rgba(34, 34, 34,1)
        lab.text = "临床试验"
        vi.addSubview(lab)
        lab.snp.makeConstraints { make in
            make.left.equalTo(imageV.snp.right).offset(SCALEWIDTH(20))
            make.centerY.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(21))
        }
        return vi
    }()
    
    
    private lazy var docufindView:UIView = {
        let vi = UIView()
        vi.backgroundColor = .white
        vi.layer.shadowColor = RGBA(r: 200, g: 200, b: 200, a: 0.5).cgColor
        vi.layer.shadowOffset = CGSize(width: 2, height: 2)
        vi.layer.shadowRadius = 4.5
        vi.layer.shadowOpacity = 1
        vi.layer.cornerRadius = 7
        vi.tag = 10088
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewAction(tap:)))
        vi.addGestureRecognizer(tap)
        let imageV = UIImageView()
        imageV.image = UIImage(named: "MYG_home_docufind")
        vi.addSubview(imageV)
        imageV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(SCALEWIDTH(18.5))
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: SCALEWIDTH(50), height: SCALEWIDTH(50)))
        }
        let lab = UILabel()
        lab.font = .medium(SCALEWIDTH(15))
        lab.textColor = .rgba(34, 34, 34,1)
        lab.text = "文献查询"
        vi.addSubview(lab)
        lab.snp.makeConstraints { make in
            make.left.equalTo(imageV.snp.right).offset(SCALEWIDTH(20))
            make.centerY.equalToSuperview()
            make.height.equalTo(SCALEWIDTH(21))
        }
        return vi
    }()
    
    
    
    
}
